package com.leetcode.algorithm.y22.m07.w5;

import com.leetcode.algorithm.common.ListNode;

/**
 * 1669. 合并两个链表
 * 
 * https://leetcode.cn/problems/merge-in-between-linked-lists/
 * 
 * @author jie.deng
 *
 */
class Question1669Solution01 {

	public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
		ListNode sentinel = new ListNode();
		sentinel.next = list1;

		// 找到第a个节点
		ListNode cur = sentinel;
		int cnt = a;
		while (cnt > 0) {
			cur = cur.next;
			cnt--;
		}
		ListNode node2Del = cur.next;

		//
		cur.next = list2;
		while (cur.next != null) {
			cur = cur.next;
		}
		ListNode tail = cur;

		cnt = b - a;
		cur = node2Del;
		while (cnt > 0) {
			cur = cur.next;
			cnt--;
		}
		tail.next = cur.next;

		return sentinel.next;
	}

}